package com.shire.shire_interface.controller.welcome;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupExample;
import com.shire.shire_interface.model.welcome.WxWelcomTask;
import com.shire.shire_interface.model.welcome.WxWelcomTaskExample;
import com.shire.shire_interface.model.welcome.WxWelcomeSetting;
import com.shire.shire_interface.service.group.WxGroupService;
import com.shire.shire_interface.service.welcome.WxWelcomeSettingService;
import com.shire.shire_interface.service.welcome.WxWelcomeTaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@Api(tags = "群管理 进群欢迎")
@RestController
@RequestMapping("/welcome")
public class WxWelcomeSettingController extends BaseController {

    @Resource
    private WxWelcomeSettingService wxWelcomeSettingService;
    @Resource
    private WxWelcomeTaskService wxWelcomeTaskService;
    @Resource
    private WxGroupService wxGroupService;


//    @ApiOperation(value = "插入设置 csid,wxaccount")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", required = true, dataType = "WxWelcomeSetting", name = "wxWelcomeSetting")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @ResponseBody
    @PostMapping("/insertSetting")
    public Response insertSetting(@RequestBody WxWelcomeSetting wxWelcomeSetting, HttpSession session){
        Response response = new Response();
        try {
            if (!checkLoginUserByCsId(session, wxWelcomeSetting.getCsid())){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            wxWelcomeSetting.setCreateTime(new Date());
            wxWelcomeSetting.setGroupids(String.join(",",(List<String>) wxWelcomeSetting.getGroupList()));
            wxWelcomeSettingService.insertSetting(wxWelcomeSetting);
            WxWelcomTask task = new WxWelcomTask();
            task.setSettingid(wxWelcomeSetting.getId());
            List<String> groupList = (List<String>) wxWelcomeSetting.getGroupList();
            for (String groupId : groupList){
                task.setWxgroupid(groupId);
                wxWelcomeTaskService.insertWelcomeTask(task);
            }

            Integer id = wxWelcomeSetting.getId();
            WxWelcomTaskExample wxWelcomTaskExample = new WxWelcomTaskExample();
            wxWelcomTaskExample.createCriteria().andSettingidEqualTo(id);
            List<WxWelcomTask> wxWelcomTasks = wxWelcomeTaskService.selectByExample(wxWelcomTaskExample);

            List<WxGroup> wxGroupList = new ArrayList<>();
            WxGroupExample wxGroupExample = new WxGroupExample();
            Integer csId = wxWelcomeSetting.getCsid();
            String wxaccountid = wxWelcomeSetting.getWxaccountid();
            for (WxWelcomTask wxWelcomTask : wxWelcomTasks) {
                wxGroupExample.clear();
                wxGroupExample.createCriteria().andCsidEqualTo(csId).andWxaccountEqualTo(wxaccountid).andGroupnoEqualTo(wxWelcomTask.getWxgroupid());
                List<WxGroup> wxGroups = wxGroupService.selectByExample(wxGroupExample);
                if (wxGroups.size()>0){
                    WxGroup wxGroup = wxGroups.get(0);
                    wxGroup.setMsgCnt(wxWelcomTask.getMsgcount());
                    wxGroupList.add(wxGroup);
                }
            }
            wxWelcomeSetting.setGroupList(wxGroupList);

            response.setData(wxWelcomeSetting);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "取消设置 settingid")
    @ResponseBody
    @PostMapping("/cancel")
    public Response cancel(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            Integer settingid = jsonObject.getInteger("settingid");
            WxWelcomeSetting wxWelcomeSettingWithBLOBs = wxWelcomeSettingService.selectByPrimaryKey(settingid);
            wxWelcomeSettingWithBLOBs.setStatus(1);
            wxWelcomeSettingService.updateByPrimaryKey(wxWelcomeSettingWithBLOBs);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "插入设置 id, csid, wxaccount")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", required = true, dataType = "WxWelcomeSetting", name = "wxWelcomeSetting")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @ResponseBody
    @PostMapping("/update")
    public Response update(@RequestBody WxWelcomeSetting wxWelcomeSetting, HttpSession session){
        Response response = new Response();
        try {
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            wxWelcomeSetting.setCreateTime(new Date());
            wxWelcomeSettingService.updateByPrimaryKey(wxWelcomeSetting);

            Integer id = wxWelcomeSetting.getId();

            WxWelcomTaskExample wxWelcomTaskExample = new WxWelcomTaskExample();
            wxWelcomTaskExample.createCriteria().andSettingidEqualTo(id);
            wxWelcomeTaskService.deleteByExample(wxWelcomTaskExample);

            List<String> groupList = (List<String>) wxWelcomeSetting.getGroupList();
            for (String groupId : groupList){
                WxWelcomTask task = new WxWelcomTask();
                task.setSettingid(id);
                task.setWxgroupid(groupId);
                wxWelcomeTaskService.insertWelcomeTask(task);
            }


            List<WxWelcomTask> wxWelcomTasks = wxWelcomeTaskService.selectByExample(wxWelcomTaskExample);
            List<WxGroup> wxGroupList = new ArrayList<>();
            WxGroupExample wxGroupExample = new WxGroupExample();
            Integer csId = wxWelcomeSetting.getCsid();
            String wxaccountid = wxWelcomeSetting.getWxaccountid();
            for (WxWelcomTask wxWelcomTask : wxWelcomTasks) {
                wxGroupExample.clear();
                wxGroupExample.createCriteria().andCsidEqualTo(csId).andWxaccountEqualTo(wxaccountid).andGroupnoEqualTo(wxWelcomTask.getWxgroupid());
                List<WxGroup> wxGroups = wxGroupService.selectByExample(wxGroupExample);
                if (wxGroups.size()>0){
                    WxGroup wxGroup = wxGroups.get(0);
                    wxGroup.setMsgCnt(wxWelcomTask.getMsgcount());
                    wxGroupList.add(wxGroup);
                }
            }
            wxWelcomeSetting.setGroupList(wxGroupList);

            response.setData(wxWelcomeSetting);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "删除设置 settingid")
    @ResponseBody
    @PostMapping("/del")
    public Response del(@RequestBody JSONObject jsonObject){
        Response response = new Response();
        try {
            Integer settingid = jsonObject.getInteger("settingid");
            wxWelcomeSettingService.deleteByPrimaryKey(settingid);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
